package com.yizhubao.app.activitys;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yizhubao.app.MainActivity;
import com.yizhubao.app.R;
import com.yizhubao.app.activitys.release.ReleaseActivity;
import com.yizhubao.app.activitys.user.ExchangeActivity;
import com.yizhubao.app.adapters.DetailUserAdapter;
import com.yizhubao.app.adapters.RecordDetailAdapter;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.base.BaseResultBean;
import com.yizhubao.app.beans.ListRecordBean;
import com.yizhubao.app.beans.RecordBean;
import com.yizhubao.app.beans.RecordDetailBean;
import com.yizhubao.app.customviews.BottomDialog;
import com.yizhubao.app.events.RecordEvent;
import com.yizhubao.app.models.RecordDetailModel;
import com.yizhubao.app.utils.Constants;
import com.yizhubao.app.utils.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.utils.ScreenUtil;
import h.framework.pacy.utils.ToastUtil;
import h.framework.pacy.views.recyclerview.DividerDecoration;

/**
 * 创建人: PACY
 * 创建时间: 6/6/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 记录详情
 */
public class RecordDetailActivity extends BaseActivityToolBar {


    private String recordId;
    @BindView(R.id.content_tv)
    TextView content_tv;

    @BindView(R.id.content_rv)
    RecyclerView content_rv;
    private RecordDetailAdapter detailAdapter;
    private LinearLayoutManager detailManager;

    @BindView(R.id.user_rv)
            RecyclerView user_rv;
    private LinearLayoutManager userManager;
    private DetailUserAdapter userAdapter;

    RecordDetailModel recordDetailModel;
    @Override
    protected int setContentLayout() {
        return R.layout.activity_record_detail;
    }


    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
        initRecyclerView();
    }

    @Override
    protected void initData() {
        recordId=getIntent().getExtras().getString("recordId");
        recordDetailModel=new RecordDetailModel();
        sendRequest();
    }

    /**
     * 获取详情
     */
    private void sendRequest(){
        showLoading();
        Map<String,Object> map=new HashMap<>();
        map.put("recordId",recordId);
        recordDetailModel.getRecordDetail(map, new BaseObserver<RecordDetailBean>() {
            @Override
            public void onSuccess(BaseResultBean<RecordDetailBean> t) {content_tv.setText(t.getData().getContent());

                detailAdapter.setNewData(t.getData().getUrlList());
                userAdapter.setNewData(t.getData().getAssignorList());
            }

            @Override
            public void onError(String errMessage) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifeSubject());
    }
    /**
     * 获取详情
     */
    private void sendDeleteRequest(){
        showLoading();
        Map<String,Object> map=new HashMap<>();
        map.put("recordId",recordId);
        recordDetailModel.deleteRecord(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                if(t.isSuccess()){
                    EventBus.getDefault().post(new RecordEvent());
                    ToastUtil.showShort("删除成功");
                    startActivity(RecordDetailActivity.this, MainActivity.class);
                }else{
                    ToastUtil.showShort(t.getMsg());
                }
            }

            @Override
            public void onError(String errMessage) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifeSubject());
    }


    private void initRecyclerView(){
        detailManager=new LinearLayoutManager(this);
        //添加分割线
        DividerDecoration decoration = new DividerDecoration(getResources().getColor(R.color.transparent), ScreenUtil.dp2px(16));
        content_rv.addItemDecoration(decoration);
        content_rv.setLayoutManager(detailManager);
        detailAdapter=new RecordDetailAdapter();
        content_rv.setAdapter(detailAdapter);

        detailAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if(view.getId()==R.id.img_iv){
                if(detailAdapter.getItem(position).getType()==1){
                    Bundle bundle=new Bundle();
                    bundle.putString("path",detailAdapter.getItem(position).getUrl());
                    startActivity(RecordDetailActivity.this,PhotoViewActivity.class,bundle);
                }else{
                    Bundle bundle=new Bundle();
                    bundle.putString("path",detailAdapter.getItem(position).getUrl());
                    startActivity(RecordDetailActivity.this,VideoActivity.class,bundle);
                }
            }
        });


        userManager=new LinearLayoutManager(this);
        user_rv.setLayoutManager(userManager);
        //添加分割线
        DividerDecoration decoration2 = new DividerDecoration(getResources().getColor(R.color.transparent), ScreenUtil.dp2px(16));
        user_rv.addItemDecoration(decoration2);
        userAdapter=new DetailUserAdapter();
        user_rv.setAdapter(userAdapter);

    }

    /**
     * 点击事件
     */
    @OnClick(value = { R.id.weituo_lin, R.id.delete_tv,R.id.exchange_tv})
    public void click(View v) {
      if (v.getId() == R.id.weituo_lin) {
            BottomDialog.DialogBuilder(RecordDetailActivity.this)
                    .title("联系人")
                    .message("①只有在确认您已经发生意外后，平台才会第一时间，将内容转交给以下所有联系人。②若没发生意外，联系人不会看到您记录的任何内容。③最少一个，最多可添加五个联系人。")
                    .completeCallBack(() -> {
                    }).completeText("知道了").othersCallBack(() -> {
                Bundle bundle = new Bundle();
                bundle.putString("title", "如何使用");
                bundle.putString("url", Constants.QUESTION);
                startActivity(RecordDetailActivity.this, WebActivity.class, bundle);
            }).cancleText("了解更多")
                    .show();
        } else if (v.getId() == R.id.delete_tv) {
          BottomDialog.DialogBuilder(RecordDetailActivity.this)
                  .title("重要提示")
                  .message("您确定要删除吗？如果删除了，在发生意外后，这份内容不会转交到联系人手中，请务必谨慎！")
                  .completeCallBack(() -> {

                  }).completeText("不删除").othersCallBack(() -> {

          }).cancleText("确定删除")
                  .othersCallBack(()->{
                      sendDeleteRequest();
                  })
                  .show();
        }else if(v.getId()==R.id.exchange_tv){
          BottomDialog.DialogBuilder(RecordDetailActivity.this)
                  .title("温馨提示")
                  .message("在您发生意外后，平台移交给联系人的，将是您最后一次修改的内容。若要修改，请认真对待 ...")
                  .completeCallBack(() -> {
                    Bundle bundle=new Bundle();
                    bundle.putString("recordId",recordId);
                    startActivity(RecordDetailActivity.this, ExchangeActivity.class,bundle);
                  }).completeText("去修改").cancleText("暂不修改")
                  .othersCallBack(()->{

                  })
                  .show();
      }
    }

}